package com.molyfun.parents.activity.common.listactivity.factory;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.listactivity.AbstractListFactory;
import com.molyfun.parents.activity.common.listactivity.IResponse;
import com.molyfun.parents.activity.common.listactivity.viewholder.CommentViewHolder;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.GetMediaCommentsResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/31/16.
 *
 * 话题评论列表
 */
public class TopicCommentFactory extends AbstractListFactory<GetMediaCommentsResponse.MediaCommentsReply> {

    private String mTopicId;
    private String mUserId;
    private boolean mIsAnonymous;

    public TopicCommentFactory(String topicId, String userId, boolean isAnonymous){
        this.mTopicId = topicId;
        this.mUserId = userId;
        this.mIsAnonymous = isAnonymous;
    }

    @Override
    public int getTitle() {
        return R.string.activity_title_topic_comment;
    }

    @Override
    public void getDataList(int pageNum,final IResponse responseCallback) {

        Call<GetMediaCommentsResponse> call = MFPApplication.getInstance().getRestClient().getMediaComments(mTopicId, 1, pageNum, RestClient.DEFAULT_PAGE_SIZE);
        call.enqueue(new Callback<GetMediaCommentsResponse>() {
            @Override
            public void onResponse(Call<GetMediaCommentsResponse> call, Response<GetMediaCommentsResponse> response) {
                if (response.body() != null && response.body().getData() != null && response.body().getData() != null) {
                    responseCallback.onResponse(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<GetMediaCommentsResponse> call, Throwable t) {
            }
        });

    }

    @Override
    public RecyclerView.ViewHolder getViewHolder(ViewGroup parent, int viewType) {
        return new CommentViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_topic_detail_comment, parent, false));
    }

    @Override
    public void bindViewHolder(final Context context, final GetMediaCommentsResponse.MediaCommentsReply replyBean, RecyclerView.ViewHolder holder, final int position, FragmentManager fragmentManager, int totalSize) {
        CommentViewHolder topicCommentViewHolder = (CommentViewHolder) holder;
        topicCommentViewHolder.mCommentCellView.initData(replyBean, mUserId, mIsAnonymous);
        topicCommentViewHolder.mCommentCellView.setBackgroundResource(R.drawable.bg_comment_single);
        topicCommentViewHolder.mCommentCellView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnListOperationCallback != null) {
                    mOnListOperationCallback.onLongClick(replyBean.getId(), position);
                }
                return false;
            }
        });
    }

    @Override
    public void deleteData(String id,final int position) {
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().delComment(id);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {

                if (mOnListOperationCallback != null) {
                    mOnListOperationCallback.onDataDelete(position);
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {

            }
        });
    }

}
